package Thread.ConsumerAndProducer.method3;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}12:18
 */
public class Storage {
    private static LinkedBlockingQueue<Object> list = new LinkedBlockingQueue<>(10);

    public void produce(){
        try {
            list.put(new Object());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("生产者"+Thread.currentThread().getName()+"生成一个产品，库存"+list.size());
    }

    public void consume()  {
        try {
            list.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("消费者"+Thread.currentThread().getName()+"消费一个产品，现库存"+list.size());
    }

    public static void main(String[] args) {
        Thread producer1 = new Thread(new Runnable() {
            @Override
            public void run() {
                new Storage().produce();
            }
        });
        Thread producer2 = new Thread(new Runnable() {
            @Override
            public void run() {
                new Storage().produce();
            }
        });
        Thread consume1 = new Thread(new Runnable() {
            @Override
            public void run() {
                new Storage().consume();
            }
        });
        producer1.start();
        producer2.start();
        consume1.start();

    }
}
